package niuke;

/**
 * Created by linshuo on 2018/2/27.
 * <p>
 * 跳台阶, 一次只能跳1或2阶, 求跳到n阶有几种跳法
 */
public class JumpFloorSimple {
    public static int jumpFloorRecur(int n) {
        if (n <= 0)
            return 0;
        if (n == 1)
            return 1;
        if (n == 2)
            return 2;
        return jumpFloorRecur(n - 1) + jumpFloorRecur(n - 2);
    }

    public static int jumpFloorIteration(int n) {
        if (n <= 0)
            return 0;
        if (n == 1)
            return 1;
        if (n == 2)
            return 2;
        int pre = 0, cur = 1;
        for (int i = 0; i < n; i++) {
            int post = pre + cur;
            pre = cur;
            cur = post;
        }
        return cur;
    }

    public static void main(String[] args) {
        System.out.println(jumpFloorRecur(9));
        System.out.println(jumpFloorIteration(9));
    }
}
